/*
 * Copyright (C) 2000-2004 Technical University of Ilmenau, Germany
 *                         Department of Electronic Circuits & Systems
 *
 * You may only use this file according to the terms of the license agreement included
 * within the file LICENSE which is distributed together with the software package.
 *
 * CVS Info:
 * $Author: rummler $
 * $Date: 2004/03/10 20:19:51 $
 * $Revision: 1.1.2.1 $
 * $State: Exp $
 * $RCSfile: TrivialSink.java,v $
 *
 */
package elka.isi.operator;

import elka.isi.util.RWUtils;
import org.evolvica.core.*;
import org.evolvica.core.common.AbstractSink;
import org.evolvica.util.EVLog;

import java.util.LinkedList;
import java.util.List;

/** Trivial sink example.
 *
 * @author Andreas Rummler
 * @since 0.4.0
 * @version $Revision: 1.1.2.1 $ $Date: 2004/03/10 20:19:51 $
 */
public class RWSink extends AbstractSink {

	/**
	 * List with contained individuals.
	 */
	protected List list = new LinkedList();
	
	/**
	 * Comparator.
	 */
	protected IIndividualComparator comparator = null;
	
	/**
	 * @see org.evolvica.engine.ISink#store(java.lang.Object)
	 */
	public void store( Object data ) {
		for ( ISetIterator it = ((IIndividualSet) data).iterator(); it.hasNext(); ) {
			store( (IIndividual) it.next() );
		}
		if ( comparator != null ) {
			//RWUtils.printIndividualSet((IIndividualSet) data);
			IIndividual bestRabbit = IndividualSetUtils.findBest( RWUtils.getRabbits((IIndividualSet) data), comparator );
			if( bestRabbit != null )
				EVLog.info( "best rabbit: genotype = " + bestRabbit.getGenotype() + ", score = " + bestRabbit.getScore() );

            IIndividual bestRoe = IndividualSetUtils.findBest( RWUtils.getRoes((IIndividualSet) data), comparator );
			if( bestRoe != null )
				EVLog.info( "best roe: genotype = " + bestRoe.getGenotype() + ", score = " + bestRoe.getScore() );

            IIndividual bestWolf = IndividualSetUtils.findBest(  RWUtils.getWolves((IIndividualSet) data), comparator );
			if( bestWolf != null )
				EVLog.info( "best wolf: genotype = " + bestWolf.getGenotype() + ", score = " + bestWolf.getScore() );
		}
		//RWUtils.printStats((IIndividualSet) data);
		
	}
	
	/**
	 * @see org.evolvica.core.common.AbstractSink#store(org.evolvica.core.IIndividual)
	 */
	public void store( IIndividual ind ) {
		list.add( ind );
	}
	
	/**
	 * @see org.evolvica.core.common.AbstractSink#toArray()
	 */
	public IIndividual[] toArray() {
		return (IIndividual[]) list.toArray( new IIndividual[list.size()] );
	}
	
	/**
	 * Returns the comparator.
	 * 
	 * @return comparator
	 */
	public IIndividualComparator getComparator() {
		return comparator;
	}

	/**
	 * Sets the comparator.
	 * 
	 * @param comparator new comparator
	 */
	public void setComparator( IIndividualComparator comparator ) {
		this.comparator = comparator;
	}
}
